package com.zhongwang.mms.module.material.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.TWarehouseInfo;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.material.model.ComplexStorageModel;
import com.zhongwang.mms.module.material.service.ComplexStorageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 库存综合查询 控制层
 * @Author: China.zy
 * @Date: 2020/3/21
 */
@Slf4j
@Controller
@RequestMapping("/matInvManage/complex")
public class ComplexStorageController extends BaseController {

    @Autowired
    private ComplexStorageService complexStorageService;
    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;

    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page/{projectCode}")
    @RequiresPermissions({"matinv:matinvquery:view"})
    public String project(@PathVariable("projectCode") String projectCode, Model model) {

        // 页面调用位置;1列表库存查询页面（projectCode = kccx） 2项目余量查询 查询按钮（projectCode = 选中行项目编号）
        if (projectCode.equals("kccx")) {
            projectCode = "";
            model.addAttribute("ityInType", "");
        } else {
            // 项目在库类型
            model.addAttribute("ityInType", 2);
        }
        model.addAttribute("projectCode", projectCode);
        List<TWarehouseInfo> warehouseInfoList = tWarehouseInfoDao.findWarehouseInfoListByWhiType("原料库");
        model.addAttribute("warehouse",warehouseInfoList );
        String warehouseStr = "";
        for (int i = 0; i < warehouseInfoList.size(); i++) {
            warehouseStr+=warehouseInfoList.get(i).getWhiId()+",";
        }
        warehouseStr=warehouseStr.substring(0,warehouseStr.length()-1);
        model.addAttribute("warehouseStr", warehouseStr);
        return "material/matInvManage/complex/page";
    }

    /**
     * 查询原料在库明细
     *
     * @param complexStorageModel 查询条件
     * @param matTypeIdList       类型Id多选
     * @param matQualityIdList    材质Id多选
     * @param matSpecIdList       规格Id多选
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(ComplexStorageModel complexStorageModel, @RequestParam(value = "matTypeIdList[]", required = false) List<String> matTypeIdList,
                           @RequestParam(value = "matQualityIdList[]", required = false) List<String> matQualityIdList,
                           @RequestParam(value = "matSpecIdList[]", required = false) List<String> matSpecIdList,
                           @RequestParam(value = "whiIdList[]", required = false) List<String> whiIdList) {
        startPage();
        if(complexStorageModel.getWhiIdListStr()!=null){
            whiIdList=Arrays.asList(complexStorageModel.getWhiIdListStr().split(","));
        }
        List<ComplexStorageModel> list = complexStorageService.findStorage(complexStorageModel, matTypeIdList, matQualityIdList, matSpecIdList,whiIdList);
        return jsonResult(list);
    }
    /**
     * 查询原料在库明细串料专用
     *
     * @param complexStorageModel 查询条件
     * @param matTypeIdList       类型Id多选
     * @param matQualityIdList    材质Id多选
     * @param matSpecIdList       规格Id多选
     * @return
     */
    @ResponseBody
    @PostMapping("/changelist")
    public JsonResult changelist(ComplexStorageModel complexStorageModel, @RequestParam(value = "matTypeIdList[]", required = false) List<String> matTypeIdList,
                           @RequestParam(value = "matQualityIdList[]", required = false) List<String> matQualityIdList,
                           @RequestParam(value = "matSpecIdList[]", required = false) List<String> matSpecIdList,
                           @RequestParam(value = "whiIdList[]", required = false) List<String> whiIdList) {
        startPage();
        if(complexStorageModel.getWhiIdListStr()!=null){
            whiIdList=Arrays.asList(complexStorageModel.getWhiIdListStr().split(","));
        }
        List<ComplexStorageModel> list = complexStorageService.findChangelistStorage(complexStorageModel, matTypeIdList, matQualityIdList, matSpecIdList,whiIdList);
        return jsonResult(list);
    }

    /**  checkProCode 根据项目号获取项信息
     * @param proCode 项目号
     * @return com.zhongwang.mms.common.JsonResult
     * @exception
     * @author 徐一贺
     * @date 2020/12/7 12:09
     */
    @ResponseBody
    @PostMapping("/checkProCode")
    public JsonResult checkProCode(String proCode){
        if(complexStorageService.getProjectByCode(proCode)!=null){
            return success();
        }
        return error();
    }

    /**
     * 查询原料在库明细
     *
     * @param complexStorageModel 查询条件
     * @param matTypeIdList       类型Id多选
     * @param matQualityIdList    材质Id多选
     * @param matSpecIdList       规格Id多选
     * @return
     */
    @PostMapping("/exportExcel")
    @ResponseBody
    @RequiresPermissions({"matinv:matinvquery:export"})
    public JsonResult exportExcel(ComplexStorageModel complexStorageModel, @RequestParam(value = "matTypeIdList[]", required = false) List<String> matTypeIdList,
                            @RequestParam(value = "matQualityIdList[]", required = false) List<String> matQualityIdList,
                            @RequestParam(value = "matSpecIdList[]", required = false) List<String> matSpecIdList,
                            @RequestParam(value = "whiIdList[]", required = false) List<String> whiIdList) {
        if(whiIdList==null){
            if (complexStorageModel.getWhiIdListStr()!=null){
                whiIdList = Arrays.asList(complexStorageModel.getWhiIdListStr().split(","));
            }
        }
       return complexStorageService.exportExcel(complexStorageModel, matTypeIdList, matQualityIdList, matSpecIdList,whiIdList);
    }

    /**
     * 首页查看明细
     * @param model
     * @param type
     * @return
     */
    @GetMapping("/lists/{type}")
    public String lists(Model model, @PathVariable("type") Integer type){
        if(type == 1){
            model.addAttribute("type", "钢材总重量");
            return "material/page";
        }else if(type == 2){
            model.addAttribute("type", "库存板材量");
            return "material/page";
        }else if(type == 3){
            model.addAttribute("type", "库存型材量");
            return "material/page";
        }else if(type == 4){
            model.addAttribute("type", "常用备件量");
            return "material/page";
        }else {
            model.addAttribute("type", "项目总重量");
            return "material/page";
        }
    }

    /**
     * 加载页面
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/index")
    public JsonResult list(ComplexStorageModel param) {

        startPage();
        List<ComplexStorageModel> list = complexStorageService.findIndexList(param);
        return jsonResult(list);
    }

}
